<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container" >
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" >
        <!-- <el-form-item label="账号" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入账号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="创建时间">
              <el-date-picker
                v-model="queryParams.createTime"
                :editable="false"
                type="daterange"
                range-separator="~"
                start-placeholder="开始时间"
                end-placeholder="截止时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item> -->

         <el-form-item label="记录类型" prop="recordType">
          <el-select
            v-model="queryParams.recordType"
            placeholder="全部"
            clearable
            style="width: 150px"
          >
          
            <el-option :label="'全部'" :value="''" />
            <el-option :label="'任务'" :value="'1'" />
            <el-option :label="'团队返佣'" :value="'2'" />
            <el-option :label="'平台'" :value="'3'" />
            <el-option :label="'积分转换'" :value="'4'" />

          </el-select>
        </el-form-item>    

        <el-form-item label="查询对象" prop="operateType">
          <el-select
            v-model="queryParams.operateType"
            placeholder="全部"
            clearable
            style="width: 150px"
          >
          
            <el-option :label="'全部'" :value="''" />
            <el-option :label="'个人'" :value="'1'" />
            <el-option :label="'团队'" :value="'2'" />

          </el-select>
        </el-form-item>    


        <el-form-item class="search-buttons">
          <el-button class="filter-item" type="primary" icon="search" @click="handleQuery">
            搜索
          </el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>


            <el-button icon="info" style="margin-left: 150px;">今日新增积分:{{ todayAdd }}</el-button>
          <el-button icon="info">昨日新增积分:{{ yesterdayAdd }}</el-button>
          <el-button icon="info">闪兑积分:{{ flashExchange }}</el-button>


        </el-form-item>
      </el-form>
    </div>
  
    <el-card shadow="hover" class="data-table">
      <el-table
        v-loading="loading"
        :data="Lists"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        class="data-table__content"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" label="序号"  />
        <!-- <el-table-column prop="userId" label="用户id"  /> -->
        <el-table-column prop="userInfo.account" label="用户账号" />
        <el-table-column prop="userInfo.nickname" label="用户昵称" />
        <el-table-column prop="userInfo.markId" label="邀请码" />
        <el-table-column prop="money" label="积分值" />

        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
              <div v-if="scope.row.status=='1'">增加</div>
              <div v-else>减少</div>
          </template>
        </el-table-column>

        <el-table-column prop="type" label="积分类型">
          <template #default="scope">
              <div v-if="scope.row.type=='1'">任务</div>
              <div v-else-if="scope.row.type=='2'">团队返佣</div>
              <div v-else-if="scope.row.type=='3'">平台</div>
              <div v-else>积分转换</div>
          </template>
        </el-table-column>

        <el-table-column prop="remark" label="备注"  />
        <el-table-column prop="createdAt" label="时间"  />

        
      </el-table>
        <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
          />

    </el-card>

   
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "积分记录",
  inheritAttrs: false,
});

import MemberApi from "@/api/system/member.api";
import { useRoute } from 'vue-router'

const route = useRoute()
    
// 获取单个参数
const id = route.query.id

const todayAdd = ref(0);
const yesterdayAdd = ref(0);
const flashExchange = ref(0);

const queryFormRef = ref();

const loading = ref(false);
const selectIds = ref<number[]>([]);

const queryParams = reactive<any>({
  pageNum: 1,
  pageSize: 10,
  recordType: "",
  operateType: "",
});

const total = ref(0);


const Lists = ref();


// 查询
function handleQuery() {
  const json = {
    page: queryParams.pageNum || 1,
    limit: queryParams.pageSize|| 10,
    userId: id || "",
    recordType:queryParams.recordType || "",
    operateType:queryParams.operateType || "",
  };
  console.log(json)
  loading.value = true;
  MemberApi.Member_score_URL(json)
    .then((res: any) => {
      console.log("查询结果:", res);
      if (res.list.total > 0) {
        Lists.value = res.list.data;
        total.value = res.list.total || 0;
        // 计算今日新增积分、昨日新增积分和闪兑积分
        todayAdd.value = res.team.todayAdd || 0;
        yesterdayAdd.value = res.team.yesterdayAdd || 0;
        flashExchange.value = res.team.flashExchange || 0;
      } else {
        Lists.value = [];
        total.value = 0;
      }
    })
    .catch((error: any) => {
      console.error("查询失败:", error);
      Lists.value = [];
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

// 处理选中项变化
function handleSelectionChange(selection: any) {
  selectIds.value = selection.map((item: any) => item.id);
}


onMounted(() => {
  handleQuery();
});
</script>
